package com.huayi.server.Controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.StandardServletEnvironment;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mac on 2017/8/28.
 */
@Controller
@RequestMapping("/admin")
public class AdminController implements EnvironmentAware {

    Logger logger = LoggerFactory.getLogger(AdminController.class);

    private Environment environment;

    @RequestMapping("/login")
    public String login(){
        return "admin/login";
    }

    @RequestMapping("/loginDo")
    public String loginDo(){
        return "redirect:/admin/main";
    }

    @RequestMapping("/main")
    public String main(){
        return "admin/index";
    }

    @RequestMapping("/welcome")
    public String welcome(Model model){

        MapPropertySource source = (MapPropertySource) ((StandardServletEnvironment) environment).getPropertySources().get("systemProperties");
        String[] names = source.getPropertyNames();
        Map<String ,String> map = new HashMap<String, String>();
        for (String name : names){
            logger.info(name+":---"+source.getProperty(name));
            map.put(name,source.getProperty(name).toString());
        }

        MapPropertySource source1 = (MapPropertySource) ((StandardServletEnvironment) environment).getPropertySources().get("systemEnvironment");
        String[] names1 = source1.getPropertyNames();
        for (String name : names1){
            if (source1.getProperty(name) == null) continue;
            map.put(name,source1.getProperty(name).toString());
        }
        logger.info(map.get("java.vm.name").toString());
        model.addAllAttributes(map);
        return "admin/welcome";
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
